package api

import (
	"api/internal/api/request"
	"api/internal/api/response"
	"api/internal/rpc"
	pp "common/consul"
	"github.com/gin-gonic/gin"
	order "order/proto"
)

func CreateOrder(c *gin.Context) {
	registration := pp.ServiceRegistration{
		Name:    "order",
		Address: "127.0.0.1",
		Port:    50052,
		Tags:    nil,
		ID:      "",
	}
	pp.RegisterService(registration)
	var json *request.OrderCreate
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, "失败", nil)
		return
	}
	createOrder, err := rpc.OrderSrv.CreateOrder(c, &order.CreateOrderRequest{
		OrderTitle:    json.OrderTitle,
		OrderPrice:    float32(json.OrderPrice),
		OrderImage:    json.OrderImage,
		OrderMessage:  json.OrderMessage,
		UserId:        int64(json.UserId),
		GoodsId:       int64(json.GoodsId),
		CouponId:      int64(json.CouponId),
		UserAddressId: int64(json.UserAddressId),
		GoodsNum:      int64(json.GoodsNum),
		PayType:       json.PayStatus,
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "成功", createOrder)
}

func UpdateOrder(c *gin.Context) {
	var json *request.OrderUpdate
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Response(c, 400, "失败", nil)
		return
	}
	createOrder, err := rpc.OrderSrv.UpdateOrder(c, &order.UpdateOrderRequest{
		OrderId:     int64(json.OrderId),
		OrderStatus: json.OrderStatus,
	})
	if err != nil {
		response.Response(c, 400, err.Error(), nil)
		return
	}
	response.Response(c, 200, "成功", createOrder)
}
